
/**
 * 悬浮按钮  返回顶部
 */
import util from "../../common/js/util.js";
var app = getApp();

let bottom = 207;

Component({
  mixins: [],
  data: {
    analyseList: {},
    resultList: [],
    showPopup: false,
  },
  props: {
    bottom,
    approvalApi: {},
    onAnalyseSucc: function onAnalyseSucc(e) { }
  },
  didMount() {
    if (util.isNotEmpty(this.props.bottom)) {
      this.setData({
        bottom: this.props.bottom
      })
    }

  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onAnalyseClick() {
      // console.log(this.props.approvalApi);
      this.setData({
        resultList:[]
      })
      dd.confirm({
        title: '提示',
        content: '是否数据分析?',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            try {
              let approvalApi = this.props.approvalApi;

              this.analyseFn(approvalApi);
              if(approvalApi.name=="price"){
                this.analyseFn(approvalApi.analysisPurchase);
              }
            } catch (error) {
              console.log(error);

            }
          }
        },
      });
    },
    analyseFn(approvalApi) {

      let obj = approvalApi.analysis;
      let url = obj.url;
      let data = obj.data;
      let dataList=approvalApi.dataList;
      let that = this;
      util.message("分析需要一段时间请稍等!");
      app.$post(url, data).then(res => {
        if (res.is == 1) {
          // 获取异常列表
          /**
           * result
           *   analyseList
           *   resultList
           */
          let result={};
          switch (approvalApi.name) {
            case "need":
              result=this.operatorNeed(res.data, that,dataList);
              if(util.isEmpty(result.resultList)){
                result.msg="所有品名的采购数量均未超过分析数量"
              }
              break;
            case "price":
             result=this.operatorNeed(res.data, that,dataList);
             if(util.isEmpty(result.resultList)){
              result.msg="所有采购单价均未超过分析单价"
            }
              break;
            case "purchase_Pact":
              result=this.operatorPact(res.data, that,dataList);

              break;
            // case "purchase_Payment":
            //   result=this.operatorPayment(res.data, that,dataList)
            //   break;
          }
          this.analyse(result,approvalApi);
        } else {
          util.message(res.data.msg);
        }
      });
    },
    // 一键判断分析
    analyse(result,approvalApi) {
      // 分析结果
      let resultStr = "";
      let resultList = this.data.resultList;
      resultList.push(...result.resultList)
      if (util.isNotEmpty(result.resultList)) {
        resultStr+="<div>分析结果:</div>"
        for (let i in result.resultList) {
          resultStr += `<p>${result.resultList[i].text}</p>`;
        }
       // console.log(result);
        util.message("分析成功!", 1000)

         this.onPubComment(resultStr,approvalApi);
      } else {
        util.message("分析结果：无异常");
        resultStr+="<div><p>分析结果:</p>";
        resultStr += `<p>${result.msg}</p></div>`;
        resultList.push(`分析结果:${result.msg}`)
        this.onPubComment(resultStr,approvalApi);
      }
      this.setData({
        resultList
      })
    },
    onClosePopup() {
      this.setData({
        showPopup: false
      })
    },
    onOpenPopup() {
      this.setData({
        showPopup: true
      })
    },
    onPubComment(comment,approvalApi) {
      // let comment = this.data.commentContent;
      if (util.isEmpty(comment)) {
        util.message("请输入评论内容!");
        return false;
      }
      //  comment = `<p>${comment}</p>`;
      let data = {
        PID: approvalApi.COMMENT_PID,
        COMMENTS: encodeURI(comment),
        TYPE_NAME: approvalApi.COMMENT_TYPE_NAME
      }
      let nodes = {};
      let that = this;
      app.$post("/YhERP/articleread/json/save/URLEncode", data).then(res => {
        if (res.is == 1) {
          // this.setData({
          //   showPopup: true
          // })
          this.onAnalyseSucc()
        };
      });
    },
    operatorNeed(data, that,dataList) {
      let analyseList = {};
      let resultList=[];
      for (let index in data) {
        analyseList[data[index].PURCHASEORDER_ID] = data[index];
      }
      //console.log(analyseList);

      if(util.isNotEmpty(analyseList)){

        for (let i in dataList) {
          let item=dataList[i];
          if(util.isNotEmpty(analyseList[item.PURCHASEORDER_ID])&&analyseList[item.PURCHASEORDER_ID].status!=1){
            let index=Number(i)+1;
            let json={index,text:`${index}:${analyseList[item.PURCHASEORDER_ID].note}`};
            resultList.push(json);
          }
        }
      }
      return {analyseList,resultList};
      // this.setData({
      //   analyseList,resultList
      // })
      //  console.log(this.data.analyseList);

    },
    operatorPact(data, that) {
      let analyseList = {};
      let resultList=[];
      for (let index in data) {
        if (data[index].note == "正常") continue;
        let sort= Number(index) + 1
        data[index].sort = sort;
        analyseList[data[index].qdID] = data[index];
        let json={index:sort,text:`<div>${sort}:${data[index].note}</div>`};
        resultList.push(json);
      }
      return {analyseList,resultList,msg:"无异常"};
      // this.setData({
      //   analyseList,
      //   resultList
      // })
    },
    operatorPrice(data, that,dataList) {
      let analyseList = {};
      let resultList=[];
      for (let index in data) {
        analyseList[data[index].PURCHASEORDER_ID] = data[index];
      }
      //console.log(analyseList);

      if(util.isNotEmpty(analyseList)){

        for (let i in dataList) {
          let item=dataList[i];
          if(util.isNotEmpty(analyseList[item.PURCHASEORDER_ID])&&analyseList[item.PURCHASEORDER_ID].status!=1){
            let index=Number(i)+1;
            let json={index,text:`<div>${index}:${analyseList[item.PURCHASEORDER_ID].note}</div>`};
            resultList.push(json);
          }
        }
      }
      return {analyseList,resultList,msg:"无异常"};
      // this.setData({
      //   analyseList,resultList
      // })
    },
    operatorPayment(data, that) {
      let analyseList = {};
      let resultList=[];
      this.setData({
        analyseList,resultList
      })
    },
    onAnalyseSucc: function onAnalyseSucc(e) {
      this.props.onAnalyseSucc(e);
    }
  },
});
